package leetcode_core.leetcode_5;

import org.junit.Test;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年01月10日 18:26
 */
public class LongestPalindrome {
    public int longestPalindrome(String s) {
        int[] smallCase = new int[26];
        int[] capitialCase = new int[26];
        int length = s.length();
        for(int i = 0;i<length;i++){
            char c = s.charAt(i);
            if('a'<= c && c<= 'z'){
                smallCase[c-'a']++;
            }else{
                capitialCase[c-'A']++;
            }
        }
        //偶数的都可以把它加到答案中
        int ans = 0;
        int odd = 0;
        for(int i = 0;i<26;i++){
            if(smallCase[i] %2 ==0){
                ans += smallCase[i];
            }else {
                if(smallCase[i] > odd){
                    if(odd != 0){
                        ans += odd -1;
                    }
                    odd = smallCase[i];
                }else{
                    ans += smallCase[i]-1;
                }
            }
            if(capitialCase[i] %2 ==0){
                ans += capitialCase[i];
            }else{
                if(capitialCase[i] > odd){
                    if(odd != 0){
                        ans += odd-1;
                    }
                    odd = capitialCase[i];
                }else{
                    ans += capitialCase[i]-1;
                }
            }
        }
        return ans + odd;
    }

    @Test
    public void test(){
        System.out.println(longestPalindrome("aaaaaccc"));
    }
}
